package com.mrd.gtimp.message.action;

import com.mrd.constant.MsgConstants;
import com.mrd.constant.WebPathConstants;
import com.mrd.exception.QueryException;
import com.mrd.exception.SaveException;
import com.mrd.gtimp.message.entity.Message;
import com.mrd.gtimp.message.entity.MessageConfig;
import com.mrd.gtimp.message.service.MessageBiz;
import com.mrd.gtimp.message.service.MessageConfigBiz;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import javax.annotation.Resource;


/**
 * Created by lenovo on 2016/9/23. 站内信 3.0
 */
@Controller
@RequestMapping("/message")
public class MessageAction {

    @Resource
    private MessageBiz messageBiz;
    @Resource
    private MessageConfigBiz messageConfigBiz;

    @RequestMapping("/msg-main")
    public String main(Model model) {
        model.addAttribute("title", "消息管理");
        model.addAttribute("left", "mes-msgMainleft");
        model.addAttribute("right", "");
        return WebPathConstants.FRAME_WEB_PATH;
    }

    /**
     * 站内列表页面
     */
    @RequestMapping(value = "msg-msgMainleft")
    private String msgMainleft() {
        return "/gtimp/message/message_left";
    }

    /**
     * 站内列表页面
     */
    @RequestMapping(value = "msg-msgMain")
    private String msgMain(Model model) {
        try {
            model.addAttribute("messageConfig", messageConfigBiz.findAllEntity());
        } catch (QueryException e) {
            e.printStackTrace();
        }
        return "/gtimp/message/message_module";
    }

    /**
     * 消息节点修改页面
     */
    @RequestMapping(value = "msg-updateMsgPage")
    private String UpdateMsgPage(Model model, String id) {
        try {
            if (StringUtils.isNotEmpty(id)) {
                model.addAttribute("mes", messageConfigBiz.findEntityById(id));
            }
        } catch (QueryException e) {
            e.printStackTrace();
        }
        return "/gtimp/message/message_edit";
    }

    /**
     * 消息节点修改操作
     */
    @RequestMapping(value = "msg-updateMsgConfig")
    @ResponseBody
    private String UpdateMsgConfig(Model model, MessageConfig messageConfig) {
        try {
            if (messageConfig != null && StringUtils.isNotEmpty(messageConfig.getId())) {
                MessageConfig oldMes = messageConfigBiz.findEntityById(messageConfig.getId());
                oldMes.setEmailStatus(messageConfig.getEmailStatus());
                oldMes.setMsgStatus(messageConfig.getMsgStatus());
                oldMes.setSmsStatus(messageConfig.getSmsStatus());
                oldMes.setStatus(messageConfig.getStatus());
                messageConfigBiz.saveOrupdateEntity(oldMes);
                return MsgConstants.OPERATION_SUCCESS;
            }
        } catch (QueryException e) {
            e.printStackTrace();
        } catch (SaveException e) {
            e.printStackTrace();
        }
        return MsgConstants.OPERATION_FAIL;
    }

    /**
     * 用户系统消息列表
     */
    @RequestMapping(value = "msg-userMsgPage")
    private String UserMsgPage(Model model) {
        try {
            model.addAttribute("userMeslist", messageBiz.findAllEntity());
        } catch (QueryException e) {
            e.printStackTrace();
        }
        return "/gtimp/message/usermessage_list";
    }

    /**
     * 用户系统消息列表
     */
    @RequestMapping(value = "msg-userMesDetail")
    private String UserMesDetail(Model model, String id) {
        try {
            if (StringUtils.isNotEmpty(id)) {
                model.addAttribute("userMes", messageBiz.findEntityById(id));
            }
        } catch (QueryException e) {
            e.printStackTrace();
        }
        return "/gtimp/message/usermessage_detail";
    }

    /**
     * 查询站内信 列表
     */
//    @RequestMapping(value = "mes-msgList")
    private List<Message> messagesConfigList() {
        return null;
    }


}
